package Algorithm.StackAndQueue;

import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

// 自己的思路
public class CatDogQueue1 {

    Deque<Pet> queue = new LinkedList<>();

    public void add(Pet pet) {
        queue.offer(pet);
    }

    public Pet pollAll() {
        if (!queue.isEmpty()) {
            return queue.poll();
        } else {
            throw new RuntimeException("Queue is empty");
        }
    }

    public Pet pollDog() {
        Stack<Pet> tempStack = new Stack<>();
        while (!queue.isEmpty() && !queue.peek().getPetType().equals("dog")) {
            tempStack.push(queue.poll());
        }
        if (!queue.isEmpty()) {
            Pet dog = queue.poll();
            while (!tempStack.isEmpty()) { // 出来的猫回去
                queue.addFirst(tempStack.pop());
            }
            return dog;
        } else {
            throw new RuntimeException("没狗了");
        }
    }

    public Pet pollCat() {
        Stack<Pet> tempStack = new Stack<>();
        while (!queue.isEmpty() && !queue.peek().getPetType().equals("cat")) {
            tempStack.push(queue.poll());
        }
        if (!queue.isEmpty()) {
            Pet dog = queue.poll();
            while (!tempStack.isEmpty()) { // 出来的狗回去
                queue.addFirst(tempStack.pop());
            }
            return dog;
        } else {
            throw new RuntimeException("没猫了");
        }
    }

    public boolean isEmpty() {
        if (queue.isEmpty()) {
            return true;
        } else {
            return false;
        }
    }

    public boolean isDogEmpty() {
        if (queue.isEmpty()) {
            return true;
        }
        Stack<Pet> tempStack = new Stack<>();
        while (!queue.isEmpty() && !queue.peek().getPetType().equals("dog")) {
            tempStack.push(queue.poll());
        }
        if (queue.isEmpty()) {
            while (!tempStack.isEmpty()) { // 出来的猫回去
                queue.addFirst(tempStack.pop());
            }
            return true;
        } else {
            while (!tempStack.isEmpty()) { // 出来的猫回去
                queue.addFirst(tempStack.pop());
            }
            return false;
        }
    }

    public boolean isCatEmpty() {
        if (queue.isEmpty()) {
            return true;
        }
        Stack<Pet> tempStack = new Stack<>();
        while (!queue.isEmpty() && !queue.peek().getPetType().equals("cat")) {
            tempStack.push(queue.poll());
        }
        if (queue.isEmpty()) {
            while (!tempStack.isEmpty()) { // 出来的猫回去
                queue.addFirst(tempStack.pop());
            }
            return true;
        } else {
            while (!tempStack.isEmpty()) { // 出来的猫回去
                queue.addFirst(tempStack.pop());
            }
            return false;
        }
    }

}
